%% 调试脚本：显示模型中所有模块的端口信息
function debug_ports(model_name)
    if nargin < 1
        model_name = 'SVG_Star_10kV_Complete';
    end
    
    % 确保模型已打开
    if ~bdIsLoaded(model_name)
        open_system(model_name);
    end
    
    fprintf('====== 调试端口信息 ======\n\n');
    
    % 获取所有顶层模块
    blocks = find_system(model_name, 'SearchDepth', 1);
    
    for i = 1:length(blocks)
        block_path = blocks{i};
        
        % 跳过模型本身
        if strcmp(block_path, model_name)
            continue;
        end
        
        block_name = get_param(block_path, 'Name');
        block_type = get_param(block_path, 'BlockType');
        
        fprintf('=====================================\n');
        fprintf('模块: %s\n', block_name);
        fprintf('类型: %s\n', block_type);
        fprintf('路径: %s\n', block_path);
        
        % 方法1: 尝试获取PortHandles
        try
            handles = get_param(block_path, 'PortHandles');
            fprintf('\nPortHandles字段:\n');
            fields = fieldnames(handles);
            for j = 1:length(fields)
                field_value = handles.(fields{j});
                if ~isempty(field_value)
                    fprintf('  %s: %d个端口\n', fields{j}, length(field_value));
                end
            end
        catch ME
            fprintf('  无法获取PortHandles: %s\n', ME.message);
        end
        
        % 方法2: 尝试获取PortConnectivity
        try
            port_conn = get_param(block_path, 'PortConnectivity');
            if ~isempty(port_conn)
                fprintf('\nPortConnectivity信息:\n');
                for j = 1:length(port_conn)
                    fprintf('  端口%d:\n', j);
                    if isfield(port_conn(j), 'Type')
                        fprintf('    Type: %s\n', port_conn(j).Type);
                    end
                    if isfield(port_conn(j), 'Position')
                        fprintf('    Position: [%.1f, %.1f]\n', port_conn(j).Position(1), port_conn(j).Position(2));
                    end
                end
            end
        catch ME
            fprintf('  无法获取PortConnectivity: %s\n', ME.message);
        end
        
        % 方法3: 对于SimPowerSystems模块，尝试特殊方法
        if contains(block_path, 'Strong_Grid') || contains(block_path, 'Weak_Grid') || ...
           contains(block_path, '_Z') || contains(block_path, 'Load')
            try
                % 尝试获取Ports参数
                ports_param = get_param(block_path, 'Ports');
                fprintf('\nPorts参数: %s\n', mat2str(ports_param));
            catch
                % 忽略
            end
            
            % 尝试获取连接信息
            try
                % 获取所有参数
                all_params = get_param(block_path, 'ObjectParameters');
                param_names = fieldnames(all_params);
                
                fprintf('\n相关端口参数:\n');
                for j = 1:length(param_names)
                    if contains(param_names{j}, 'Port') || contains(param_names{j}, 'Conn')
                        try
                            value = get_param(block_path, param_names{j});
                            if ~isempty(value)
                                fprintf('  %s: %s\n', param_names{j}, mat2str(value));
                            end
                        catch
                            % 忽略
                        end
                    end
                end
            catch
                % 忽略
            end
        end
        
        % 方法4: 对于子系统，检查内部端口
        if strcmp(block_type, 'SubSystem')
            try
                in_ports = find_system(block_path, 'SearchDepth', 1, 'BlockType', 'Inport');
                out_ports = find_system(block_path, 'SearchDepth', 1, 'BlockType', 'Outport');
                
                if length(in_ports) > 1 || length(out_ports) > 1
                    fprintf('\n子系统内部端口:\n');
                    fprintf('  Inport数量: %d\n', length(in_ports) - 1);
                    fprintf('  Outport数量: %d\n', length(out_ports) - 1);
                end
            catch
                % 忽略
            end
        end
        
        fprintf('\n');
    end
    
    fprintf('\n====== 端口调试完成 ======\n');
end